<template>

	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">
			<el-form-item prop="code" label="代码">
				<el-input size="small" v-model="dataForm.code" placeholder="代码"></el-input>
			</el-form-item>
			<el-form-item prop="codename" label="代码名称">
				<el-input size="small" v-model="dataForm.codename" placeholder="代码名称"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button size="small" @click="visible = false">取消</el-button>
			<el-button size="small" type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		validEmail,
		isMobile
	} from "@/utils/validate";
	import {
		getDeptLists
	} from "@/network/sys/dept";
	import {
		getCodeById,
		putCode,
		postCode
	} from "@/network/lswms/base/Code"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getRoles
	} from "@/network/sys/role";
	import _ from 'lodash'


	export default {
		name: "CodeAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			// const deptList = ref([]);
			// const deptListVisible = ref(false);
			// const roleList = ref([]);
			// const roleIdListDefault = ref([]);
			const dataForm = ref({
				id: "",
				code: '',
				name: ''
			});


			const dataRule = reactive({
				code: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				codename: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}]
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getCodeInfo();
					}

				})
			}

			const getCodeInfo = () => {
				getCodeById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postCode(dataForm.value) : putCode(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				// deptList,
				// deptListVisible,
				// roleList,
				// roleIdListDefault,
				dataForm,
				dataRule,
				// validatePassword,
				// validateComfirmPassword,
				// validateEmail,
				// validateMobile,
				init,
				// getDeptList,
				// getRoleList,
				// getInfo,
				// deptListTreeCurrentChangeHandle,
				dataFormSubmitHandle,
				dataFormRef

    }
  },

		beforeCreate: function() {
			self = this;
		},

	}
</script>

<style scoped>

</style>
